package com.sphd.emanu.fragment.day;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.cardnum.CardNumFragment;
import com.sphd.emanu.fragment.search.SearchFragment;
import com.sphd.emanu.fragment.time.TimeFragment;
import com.sphd.emanu.interfaces.CardFragmentLetter;
import com.sphd.emanu.interfaces.Letter;
import com.sphd.emanu.interfaces.LetterAtoF;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.MonthOrYearInfo;
import com.sphd.emanu.pojo.login.User;
import com.wzutils.TimeUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class DayFragment extends MVPBaseFragment<DayContract.View, DayPresenter> implements DayContract.View, Letter, CardFragmentLetter {
    @BindView(R.id.financial_balance_text)
    TextView financialBalanceText;
    @BindView(R.id.financial_balance_hint)
    TextView financialBalanceHint;
    private List<Fragment> list_fragment;//定义要装fragment的列表
    CardNumFragment cardNumFragment;
    TimeFragment timeFragment;
    MyViewPagerAdapter myViewPagerAdapter;
    @BindView(R.id.go_back_img)
    ImageView goBackImg;
    @BindView(R.id.today_radio_io)
    RadioButton todayRadioIo;
    @BindView(R.id.yestaday_radio_io)
    RadioButton yestadayRadioIo;
    @BindView(R.id.last_month_radio_io)
    RadioButton lastMonthRadioIo;
    @BindView(R.id.now_month_radio_io)
    RadioButton nowMonthRadioIo;
    @BindView(R.id.radio_group_for_month_year)
    RadioGroup radioGroupForMonthYear;
    Unbinder unbinder;
    Bundle bundle;
    @BindView(R.id.show_time_cardnum_tablayout)
    TabLayout tabs;
    @BindView(R.id.vp_Fragment_pager)
    ViewPager vpFragmentPager;
    private int getIdFrom = 0;
    User user;
    int type = 0;
    String beginTime, endTime;
    private LetterAtoF letterAtoF;

    public void setLetterAtoF(LetterAtoF letterAtoF) {
        this.letterAtoF = letterAtoF;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_day;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        getIdFrom = bundle.getInt(Constant.R_ID);
        initRadioGroup();
        initTime();
        //初始化显示方式
        initFragment();
        initEvent();
        initTopData();
        setUserVisibleHint(true);
    }

    private void initTopData() {
        showProgressDialog();
        mPresenter.getAllAccountData(setMapMessage());


    }
    public Map setMapMessage() {
        Map<String, String> map = new HashMap<>();
        map.put("oid", String.valueOf(user.getOid()));
        map.put("state", String.valueOf(type == 0 || type == 1 ? 4 : type == 2 ? 2 : 1));
        map.put("beginDate", beginTime);
        map.put("endDate", endTime);
        map.put("monthOrDay", "2");
        return map;
    }

    private void initTime() {
        if (type == 0) {
            beginTime = TimeUtils.longToString(TimeUtils.systemTimeLong(), TimeUtils.YEAR_MONTH_DAY);
            endTime = beginTime;

        } else if (type == 1) {
            beginTime = TimeUtils.getYesterday(TimeUtils.YEAR_MONTH_DAY);
            endTime = beginTime;

        } else if (type == 2) {
            try {
                String[] times = TimeUtils.getLastMonthFirstLastday(Calendar.getInstance(), TimeUtils.systemTimeLong());
                beginTime = times[0];
                endTime = times[1];
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (type == 3) {

            try {
                beginTime = TimeUtils.getNowMonthFirstLastday(Calendar.getInstance(), TimeUtils.systemTimeLong())[0];
            } catch (ParseException e) {
                e.printStackTrace();
            }
            endTime = TimeUtils.longToString(TimeUtils.systemTimeLong(), TimeUtils.YEAR_MONTH_DAY);
        }


    }

    private void initRadioGroup() {
        switch (getIdFrom) {
            //用来判断是本月还是上月
            case R.id.today_radio_io:
                setRadioStyle(true, false, false, false);
                type = 0;
                break;
            case R.id.yestaday_radio_io:
                setRadioStyle(false, true, false, false);
                type = 1;
                break;
        }
    }

    private void initFragment() {
        list_fragment = new ArrayList<>();
        timeFragment = new TimeFragment();
        cardNumFragment = new CardNumFragment();
        bundle.putString(Constant.CLASS_NAME, SearchFragment.class.getName());
        timeFragment.setArguments(bundle);
        cardNumFragment.setArguments(bundle);
        timeFragment.setLetter(this);
        cardNumFragment.setCardFragmentLetter(this);
        list_fragment.add(timeFragment);
        list_fragment.add(cardNumFragment);
        String titles[] = {"按时间查看", "按账户查看"};
        myViewPagerAdapter = new MyViewPagerAdapter(context, getChildFragmentManager(), list_fragment, titles);
        tabs.setupWithViewPager(vpFragmentPager);
        vpFragmentPager.setAdapter(myViewPagerAdapter);
    }


    private void initEvent() {
        radioGroupForMonthYear.setOnCheckedChangeListener(
                (group, checkedId) -> {
                    View viewById = radioGroupForMonthYear.findViewById(checkedId);
                    if (!viewById.isPressed()) {
                        return;
                    }
                    switch (checkedId) {
                        case R.id.today_radio_io://昨日收入或者支出
                            setRadioStyle(true, false, false, false);
                            type = 0;
                            break;
                        case R.id.yestaday_radio_io://今日收入或者支出
                            setRadioStyle(false, true, false, false);
                            type = 1;
                            break;
                        case R.id.last_month_radio_io://上月收入或者支出
                            setRadioStyle(false, false, true, false);
                            type = 2;
                            break;
                        case R.id.now_month_radio_io://本月收入或者支出
                            setRadioStyle(false, false, false, true);
                            type = 3;
                            break;
                    }
                    initTime();
                    initTopData();
                    readyed();
                    readyedCardFragment();
                }
        );
    }

    private void setRadioStyle(boolean b, boolean b1, boolean b2, boolean b3) {
        todayRadioIo.setBackground(context.getDrawable(b ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        todayRadioIo.setTextColor(ContextCompat.getColor(context, b ? R.color.colorPrimary : R.color.white));

        yestadayRadioIo.setBackground(context.getDrawable(b1 ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        yestadayRadioIo.setTextColor(ContextCompat.getColor(context, b1 ? R.color.colorPrimary : R.color.white));

        lastMonthRadioIo.setBackground(context.getDrawable(b2 ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        lastMonthRadioIo.setTextColor(ContextCompat.getColor(context, b2 ? R.color.colorPrimary : R.color.white));

        nowMonthRadioIo.setBackground(context.getDrawable(b3 ? R.drawable.radio_check_true : R.drawable.radio_check_false));
        nowMonthRadioIo.setTextColor(ContextCompat.getColor(context, b3 ? R.color.colorPrimary : R.color.white));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.go_back_img)
    public void onViewClicked() {
        letterAtoF.finishActivity();
    }

    @Override
    public void readyed() {
        timeFragment.swipeRefreshData(type == 0 || type == 1 ? 4 : type == 2 ? 2 : 1, 0, beginTime, endTime);
    }

    @Override
    public void readyedCardFragment() {
        cardNumFragment.swipeRefreshData(type == 0 || type == 1 ? 4 : type == 2 ? 2 : 1, beginTime, endTime);
    }


    @Override
    public void setBean(MonthOrYearInfo monthOrYearInfo) {
        dissProgressDialog();
        financialBalanceText.setText(monthOrYearInfo.getAllBalance() + "");
    }
}
